<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>
<html>
<head>
    <title>分配条形码</title>
    <meta name="decorator" content="default"/>
    <script type="text/javascript">
        document.onkeydown = function (event) {
            var e = event || window.event || arguments.callee.caller.arguments[0];
            if (e && e.keyCode == 13) {
                var flag = true;
                $("input[name='banId']").each(function(i){
                    if ($(this).val() == '') {
                        $(this).focus();
                        flag = false;
                        return false;
                    }
                    if(flag){
                        $("#callBackBanId_btn").focus();
                    }
                });
            }
        };
        $(function(){
            var banIds = '${banids}'.split(",");
            $("input[name='banId']").each(function(i){
                if(i == 0){
                    $(this).focus();
                }
                $(this).val(banIds[i].trim());
            });
        });
        function callBackBanId() {
            var flag = true;
            var banIds = "";
            $("input[name='banId']").each(function (i) {
                var banId = $(this).val();
                if (banId == '') {
                    $(this).css("color", "red").css("border", "1px solid red");
                    $(this).siblings("label").html("条形码必填");
                    flag = false;
                } else {
                    $(this).css("color", "black").css("border", "1px solid #ccc");
                    $(this).siblings("label").html("");
                    if (i == 0) {
                        banIds = banId.trim();
                    } else {
                        banIds += "," + banId.trim();
                    }
                }
            });

            if(flag){
                $.post("${ctx}/book/store/checkRepeatBanId", {banIds: banIds}, function (result) {
                    if (result.code == '0001') {
                        window.parent.banIds = banIds;
                        window.parent.window.jBox.close();
                    }else{
                        var data = result.data;
                        for(var i=0;i<data.length;i++){
                            $("input[name='banId']").each(function () {
                                $(this).val($(this).val().trim());

                                if($(this).val().trim()==data[i]){
                                    $(this).css("color", "red").css("border", "1px solid red");
                                    $(this).siblings("label").html("条形码重复");
                                }
                            });
                        }
                    }
                });
            }
        }
        function creatBan(){
            var firstBanId = $("#banId_1").val();
            if(firstBanId == ''){
                $.ajax({
                    type: "POST",
                    url: "${ctx}/book/store/myLastBanId",
                    dataType: "json",
                    async:false,
                    success: function (result) {
                        firstBanId = result.data;
                        firstBanId = parseInt(firstBanId)+1;
                    }
                });
            }else{
                firstBanId = parseInt(firstBanId);
            }
            for(var i =0;i< ${amount};i++){
                $("#banId_"+(i+1)).val(firstBanId+i);
            }
        }
    </script>
    <style>
        .inp_ln {
            width: 150px !important;
            height: 15px !important;
            font-size: 12px !important;
        }
        .table-condensed th, .table-condensed td{
            padding: 0 5px;
        }
    </style>
</head>
<body>
<table id="contentTable" class="table-striped table-bordered table-condensed" style="margin-left: 5px;" width="100%">
    <c:forEach var="index" begin="1" end="${amount}" varStatus="status">
        <tr align="left">
            <td>
                <input id="banId_${status.index}" name="banId" class="inp_ln" type="text"/>
                <label style="color: red;"></label>
                <c:if test="${status.index==1}">
                    <a class="btn btn-small" href="javascript:void(0);" onclick="creatBan()">自动生成</a>
                </c:if>
            </td>
        </tr>
    </c:forEach>
    <tr align="center">
        <td>
            <input id="callBackBanId_btn" type="button" class="btn btn-primary submit" onclick="callBackBanId()" value="确定"/>
        </td>
    </tr>
</table>
</body>
</html>